﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonUtils.Helpers
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// DateTime转时间戳 
        /// </summary>
        /// <param name="dt">DateTime</param>
        /// <returns>时间戳（秒）</returns>
        public static long ToUnixTimeStamp(this DateTime dt)
        {
            return (dt.ToUniversalTime().Ticks - 621355968000000000) / TimeSpan.TicksPerMillisecond;
        }

        /// <summary>
        /// 时间戳转DateTime
        /// </summary>
        /// <param name="timeStamp">时间戳（秒）</param>
        /// <returns>DateTime</returns>
        public static DateTime GetDateTimeFromUnixTimeStamp(long timeStamp)
        {
            return new DateTime(621355968000000000 + timeStamp * TimeSpan.TicksPerMillisecond).ToLocalTime();
        }
    }
}
